package com.auto.autoapp.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.fragment.app.Fragment
import com.auto.autoapp.widgets.SimpleToolbar

abstract class BaseFragment : Fragment() {
    open fun showToolbar(): Boolean {
        return true
    }

    abstract fun onInit()

    open fun configToolbar(toolbar: SimpleToolbar) {}
    abstract fun provideView(): View
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val contentView = provideView()
        val rootView = if (showToolbar()) {
            val layout = LinearLayoutCompat(requireContext())
            layout.orientation = LinearLayoutCompat.VERTICAL
            val toolbar = SimpleToolbar(requireContext())
            configToolbar(toolbar)
            layout.addView(toolbar)
            val layoutParams =
                LinearLayoutCompat.LayoutParams(
                    LinearLayoutCompat.LayoutParams.MATCH_PARENT,
                    LinearLayoutCompat.LayoutParams.MATCH_PARENT
                )
            layoutParams.weight = 1.0f
            layout.addView(contentView, layoutParams)
            layout
        } else {
            contentView
        }
        onInit()
        return rootView
    }
}